const express = require('express')
const axios = require('axios')
const users = express.Router()

//分页查询普通用户信息
users.get('/User/queryForUserMessage', (req, res) => {
    let { pageNow, pageSize } = req.query
    axios({
        method: 'get',
        url: '/Admin/User/queryForUserMessage',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params: {
            pageNow: req.query.pageNow || 1,
            pageSize: req.query.pageSize || 6
        }
    }).then(value => {
        res.status(200).json({
            key: value.data
        })
    }).catch(err => {
        res.status(200).json({
            error: err
        })
    })
})

//根据id查询普通用户信息
users.get('/User/queryForOne', (req, res) => {
    let { id } = req.query
    axios({
        method: 'get',
        url: '/Admin/User/queryForOne',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params: {
            id: id
        }
    }).then(value => {
        res.status(200).json({
            key: value.data
        })
    }).catch(err => {
        res.status(200).json({
            error: err
        })
    })
})


//修改普通用户信息
users.get('/User/putForOne', (req, res) => {
    let { id, account, album, area, email, fans, names, password, portrait, resume, sex, age, multipartFile } = req.query
    axios({
        method: 'get',
        url: '/Admin/User/queryForOne',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params: {
            id: id
        }
    }).then(value => {
        let message = value.data.data
        let accountv = message.account //账号
            // console.log(account);
        let namev = message.name //昵称
            // console.log(name);
        let sexv = message.sex //性别
            // console.log(sex);
        let agev = message.age //年龄
            // console.log(age);
        let portraitv = message.portrait //头像
            // console.log(portrait);
        let areav = message.area //地区
            // console.log(area);
        let emailv = message.email //邮箱
            // console.log(email);
        let resumev = message.resume //简介
            // console.log(resume);
        let fansv = message.fans //粉丝
            // console.log(fans);
        let albumv = message.album //专辑
            // console.log(album);
            // console.log(value.data);
        if (value.data.message == "成功") {
            axios({
                method: 'put',
                url: '/Admin/User/putForOne',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                    token:(req.session.token?req.session.token:'')
                },
                params: {
                    id: id,
                    account: account || accountv,
                    age: age || agev,
                    album: album || albumv,
                    area: area || areav,
                    email: email || emailv,
                    fans: fans || fansv,
                    name: names || namev,
                    password: password || "",
                    portrait: portrait || portraitv,
                    resume: resume || resumev,
                    sex: sex || sexv,
                    multipartFile: "D:\前端\background\me.jpg"
                }
            }).then(data => {
                // console.log(data.data);
                console.log("正确");
                res.status(200).json({
                    key: data.data
                })
            }).catch(error => {
                console.log("错误");
                // console.log(error);
                res.status(200).json({
                    error: "错误"
                })
            })
        }
    }).catch(err => {
        console.log("错误1");
        res.status(200).json({
            error: "错误1"
        })
    })
})


//搜索普通用户
users.get('/User/query', (req, res) => {
    let { pageNow, pageSize, name } = req.query
    axios({
        method: 'get',
        url: '/Admin/User/query',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token:(req.session.token?req.session.token:'')
        },
        params: {
            pageNow: req.query.pageNow || 1,
            pageSize: req.query.pageSize || 6,
            name: name
        }
    }).then(value => {
        res.status(200).json({
            key: value.data
        })
    }).catch(err => {
        res.status(200).json({
            error: err
        })
    })
})





module.exports = users